package com.unieagle.exam.ui.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.unieagle.exam.R;
import com.unieagle.exam.share.ShareHelper;


public class ShareGridDialog extends Dialog {
	
	private Activity ac;

    public ShareGridDialog(Context context) {
        super(context,R.style.share_dialog_content);
    }
    
    public void setActivity(Activity ac) {
    	this.ac = ac;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share_content);
        getWindow().setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        getWindow().setWindowAnimations(R.style.Anim_dialog_Window);
        GridView gridView = (GridView) findViewById(R.id.share_grid_view);
        findViewById(R.id.share_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        final ShareAdapter adapter = new ShareAdapter(getContext());
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShareHelper.doShare(ac, position);
                dismiss();
            }
        });
    }


    public static class ShareAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater mInflater;
        String[] mShareAppNames;
        int[] mShareAppIcons;

        public ShareAdapter(Context ctx) {
            mContext = ctx;
            mInflater = LayoutInflater.from(mContext);
            Resources resources = mContext.getResources();
            mShareAppNames = resources.getStringArray(R.array.share_app_names);
            mShareAppIcons = new int[]{R.drawable.info_share_wechat, R.drawable.info_share_wechatmoments, R.drawable.info_share_qq
                    , R.drawable.info_share_qzone};
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.view_share_gridview_item, null);
                viewHolder.info_share_gridview_item_app_icon = (ImageView) convertView
                        .findViewById(R.id.info_share_gridview_item_app_icon);
                viewHolder.info_share_gridview_item_text = (TextView) convertView
                        .findViewById(R.id.info_share_gridview_item_text);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.info_share_gridview_item_app_icon.setImageResource(mShareAppIcons[position]);
            viewHolder.info_share_gridview_item_text.setText(mShareAppNames[position]);
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            return mShareAppNames.length;
        }

        private class ViewHolder {
            ImageView info_share_gridview_item_app_icon;
            TextView info_share_gridview_item_text;
        }
    }

}
